package com.example.base.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.base.Utils.OkHttpUtil;
import com.example.base.activity.SystemActivity;
import com.example.base.adapter.FlowAdapter;
import com.example.base.R;
import com.example.base.bean.Home;
import com.example.base.view.SysViewLayout;
import com.example.base.bean.SystemOne;
import com.example.base.bean.SystemTwo;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SystemFragment extends Fragment {
  private Context context;
  private FlowAdapter flowAdapter;
  private ListView listView;
  private List<SystemTwo> dataList = new ArrayList<>();
  SystemOne systemOne;

  public SystemFragment() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View inflate = inflater.inflate(R.layout.fragment_system, container, false);
    initView(inflate);
    initListener();
    initSystemView();
    sendRequestWithokhttp();
    return inflate;
  }


  private void initView(View inflate) {
    listView = inflate.findViewById(R.id.listview_sys);
    context = getContext();
  }

  private void initListener() {
    ListViewClickListener();
  }

  private void ListViewClickListener() {
    sendRequestWithokhttp();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int number = position;
        String title = systemOne.getData().get(position).getName();
        Intent intent = new Intent(context, SystemActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("number", number);
        startActivity(intent);
      }
    });
  }

  private void sendRequestWithokhttp() {
    OkHttpUtil.getAsync("https://www.wanandroid.com/tree/json", new OkHttpUtil.DataCallBack() {
      @Override
      public void requestFailure(Request request, IOException e) {

      }

      @Override
      public void requestSuccess(String result) throws Exception {
        Gson gson = new Gson();
        systemOne = gson.fromJson(result, SystemOne.class);
        refreshListView(systemOne);
      }
    });
  }

  private void refreshListView(SystemOne systemOne) {
    List<SystemTwo> data = systemOne.getData();
    flowAdapter.setData(data);
    flowAdapter.notifyDataSetChanged();
  }

  private void initSystemView() {
    flowAdapter = new FlowAdapter(context, dataList);
    listView.setAdapter(flowAdapter);
  }

}